package Client_Module;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client_Waiter implements Runnable {
    public Socket socket_client;
    public boolean force_stop = false;
    public Scanner SCinput = new Scanner(System.in);
    public int do_number = 0;
    private boolean pressure_test_mode = false;
    private Thread ReadData;
    private DataInputStream indatastream = null;
    private String temp = "";

    public Client_Waiter(String ip, int port) {
        try {
            socket_client = new Socket(ip, port);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.print("请输入你要操作的序号(1.手动模式 2.压力测试 3.退出系统 ):");
        do_number = SCinput.nextInt();// 输入运行模式
        if (do_number == 1) {
        }
        if (do_number == 2) {
            pressure_test_mode = true;
        }
        if (do_number == 3) {
            return;
        }
    }

    public void run() {
        try {

            InputStream indata = socket_client.getInputStream();
            indatastream = new DataInputStream(indata);
            // Read();
            // ReadData.start();
            // output("connect");
            // temp = indatastream.readUTF();
            // System.out.println(temp);
            while (true) {
                if (pressure_test_mode == false) {
                    System.out.print("请输入你要操作的序号(1.手动取号 2.退出系统):");
                    // if (do_number != 2) {
                    do_number = SCinput.nextInt();// 输入模式
                    // } // 压力测试完毕 把注释去除}
                    if (do_number == 1) {
                        output("putin");

                    }
                    if (do_number == 2) {
                        break;
                    }
                    temp = indatastream.readUTF();
                    clear_control_board();
                    System.out.println(temp);
                } else {
                    try {
                        pressure_test(1200, "");// 用于压力测试
                    } catch (InterruptedException e) {
                    }
                }

                // try {
                // Thread.sleep((int) (Math.random() * 3000));
                // } catch (InterruptedException e) {
                // e.printStackTrace();
                // }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void Read() {
        ReadData = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    temp = indatastream.readUTF();
                } catch (IOException e) {
                    e.printStackTrace();
                } // 接收数据
                if (temp != "") {
                    try {
                        clear_control_board();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    checkDown(temp);
                    // ToResponse(temp);
                    System.out.println(temp);
                }
            }
        });
    }

    public void output(String text) {// 发送数据
        try {
            OutputStream outdata = socket_client.getOutputStream();
            DataOutputStream outdatastream = new DataOutputStream(outdata);
            outdatastream.writeUTF(text);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void pressure_test(int time, String text) throws InterruptedException { // 用于压力测试
        if (pressure_test_mode == false) {
            return;
        }
        output("putin");
        Thread.sleep(time);
    }

    private void clear_control_board() throws IOException {
        System.out.print("\033[H\033[2J");
        System.out.flush();
    }

    private void checkDown(String temp) {
        if (temp.equals("ShutdownNow")) {
            System.exit(0);
        }
    }
}
